package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.Color;
import org.example.erp.service.ColorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ColorController {
    @Autowired
    ColorService colorService;

    @GetMapping("/color/find")
    public DataResult getColorList(@RequestParam String colorCode,
                                   @RequestParam String color,
                                   @RequestParam String remarks,
                                   @RequestParam(defaultValue = "1") int currentPage,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        Page<Color> page = new Page<Color>(currentPage, pageSize);
        QueryWrapper<Color> queryWrapper = new QueryWrapper();
        if (colorCode != null && !"".equals(colorCode)) {
            queryWrapper.lambda().like(Color::getColorCode, colorCode);
        }
        if (color != null && !"".equals(color)) {
            queryWrapper.lambda().like(Color::getColor, color);
        }
        if (remarks != null && !"".equals(remarks)) {
            queryWrapper.lambda().like(Color::getRemark, remarks);
        }
        page = colorService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @PostMapping("/color/insert")
    public DataResult addFind(@RequestBody Color form) {
        boolean save = colorService.save(form);
        return DataResult.success(save ? "成功" : "失败");
    }

    @PutMapping("/color/update")
    public DataResult updateFind(@RequestBody Color form) {
        boolean update = colorService.updateById(form);
        return DataResult.success(update ? "成功" : "失败");
    }

    @DeleteMapping("/color/delete")
    public DataResult deleteFind(@RequestBody List<String> id) {
        boolean delete = colorService.removeByIds(id);
        return DataResult.success(delete ? "成功" : "失败");
    }
}
